<?php

//管理员添加新闻接口
require_once '../utils/response.php';
require_once  '../utils/db.php';

header('content-type:text/html;charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET,POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$name = json_decode(file_get_contents("php://input"))->name;
$title = json_decode(file_get_contents("php://input"))->title;
$content = json_decode(file_get_contents("php://input"))->content;
date_default_timezone_set('Asia/Shanghai');
$time = date('Y-m-d H:i:s', time());

// if (empty($name) || empty($content)) {
//     return Response::show(401, '数据不合法');
// }

try {
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403, '数据库连接失败', $e->getMessage());
}


$sql = "INSERT msg VALUES(NULL,?,?,?,?)";
$mysqli_stmt = $connect->prepare($sql);
$mysqli_stmt->bind_param('ssss',$name, $title, $content, $time);

$data = array(
    'IsSuccess' => false,
    'InsertId' => ''
);

// echo $mysqli_stmt;
// exit;

if ($mysqli_stmt->execute()) {
    $data['IsSuccess'] = true;
    $data['InsertId'] = $mysqli_stmt->insert_id;
    return Response::show(200, '留言上传成功', $data);
} else {
    return Response::show(404, '留言上传失败', $data);
}
